//
//  HHVideoPlayController.swift
//  HHSwift
//
//  Created by qk-huahong on 2019/6/27.
//  Copyright © 2019 huahong. All rights reserved.
//

import UIKit

class HHVideoPlayController: UIViewController {

    @IBOutlet weak var timeSlider: UISlider!
    @IBOutlet weak var startTimeLabel: UILabel!
    @IBOutlet weak var durationLabel: UILabel!
    @IBOutlet weak var rewindButton: UIButton!
    @IBOutlet weak var playPauseButton: UIButton!
    @IBOutlet weak var fastForwardButton: UIButton!
    @IBOutlet weak var playerView: PlayerView!
    
    
    var player:HHPlayer!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        title = "视频播放"

        let movieURL = Bundle.main.url(forResource: "video", withExtension: "mov")
        
        guard let URL = movieURL else { return  }
        
        self.player = HHPlayer()
        self.player.assetURL = URL
        self.player.container = self.playerView
        self.player.prepareToPlay()
        self.player.playProgressBlock = {[unowned self] (current,total) in
            
            self.timeSlider.value = current
            self.startTimeLabel.text = self.createTimeString(time: current)
            self.timeSlider.maximumValue = total
            self.timeSlider.value = current
            self.durationLabel.text = self.createTimeString(time: total)
        }
        
        self.player.playEndBlock = { [weak self] in
            print("播放结束")
            self?.playPauseButton.setImage(UIImage.init(named: "PlayButton"), for: .normal)

        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        self.player.pause()
        
    }
    
    
    // MARK: - IBActions
    
    @IBAction func playPauseButtonWasPressed(_ sender: UIButton) {
        if player.rate > 0.0 {
            
           player.pause()
           sender.setImage(UIImage.init(named: "PlayButton"), for: .normal)
            
        }else {
            
            player.play()
            sender.setImage(UIImage.init(named: "PauseButton"), for: .normal)
            
        }
    }
    
    @IBAction func rewindButtonWasPressed(_ sender: UIButton) {

        self.player.rate = -2.0
    }
    
    @IBAction func fastForwardButtonWasPressed(_ sender: UIButton) {

        self.player.rate = 2.0
    }
    
    @IBAction func timeSliderDidChange(_ sender: UISlider) {

        self.player.seek(to: sender.value) {[weak self] (complate) in
            if complate == true {
                self?.player.play()
            }
            
        }
    }
    
    

    // MARK: Convenience
    
    func createTimeString(time: Float) -> String {
        var components = DateComponents()
            components.second = Int(max(0.0, time))
        let formatter = DateComponentsFormatter()
            formatter.zeroFormattingBehavior = .pad
            formatter.allowedUnits = [.minute, .second]
        return formatter.string(from: components)!
    }
}
